import { observable, action, runInAction } from 'mobx'
import { post } from 'lib/net'
import CommonVM from 'cvm/common'
import * as api from 'lib/constants/api'

export default class ReplyMe extends CommonVM {
  constructor (props) {
    super()
    this.props = props
    this.fetchData()
  }

  // 数据
  @observable data = []

  // 加载更多
  @action handleLoadMore = () => {
    this.fetchData()
  }

  // 下拉刷新
  @action handleRefresh = resolve => {
    this.pageInfo.page = 1
    this.fetchData(resolve)
  }

  // 获取提及回复
  @action fetchData = async (resolve) => {
    this.fetchPage()
    let params = {
      UserInfoID: this.userId,
      Offset: this.pageInfo.page,
      Limit: this.pageInfo.size,
      TimeStamp: this.pageInfo.timeStamp
    }
    let res = await post(api.OTHER_REPLY, params)
    if (res.state === 1) {
      runInAction(() => {
        if (this.pageInfo.page === 1) {
          this.data.splice(0, this.data.length, ...(res.data || []))
        } else {
          this.data.splice(this.data.length, 0, ...(res.data || []))
        }
        this.receivePage(res.data)
      })
    } else {
      this.receivePage()
    }
    if (resolve) {
      resolve()
    }
  }
}
